package com.warm.pump.module.monitor.controller;

import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.frame.gen.util.MyBeanUtils;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.frame.util.StringUtils;
import com.warm.pump.module.monitor.bean.po.gen.MonitorCache;
import com.warm.pump.module.monitor.service.IMonitorCacheService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * monitor_cache
 */
@Controller
@RequestMapping("/monitorCache")
public class MonitorCacheController {
    @Autowired
    private IMonitorCacheService monitorCacheService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public OutJson list(Integer page, Integer limit, MonitorCache monitorCache) {
        Map<String, Object> map = new HashMap<>();
        MyBeanUtils.copyBean2Map(map, monitorCache);
        map.put("startDate_lessEqual", new Date());
        map.put("expireDate_greaterEqual", new Date());

        if (StringUtils.isNotEmpty(monitorCache.getSidx()) && StringUtils.isNotEmpty(monitorCache.getOrder())) {
            map.put("orderBy", monitorCache.getSidx() + " " + monitorCache.getOrder());
        }
        //查询列表数据
        PageBean pageBean = monitorCacheService.getPageBeanByMap(map, page, limit);

        return OutJson.ok().put("page", pageBean);
    }


    @ResponseBody
    @RequestMapping("/cacheKeys")
    public OutJson cacheKeys(MonitorCache monitorCache, String keyName) {

        List<Map<String, Object>> cacheKeys = monitorCacheService.getCacheKeys(monitorCache.getCacheName(), keyName);

        return OutJson.ok().setResData(cacheKeys);
    }

    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/getCacheInfo")
    public OutJson getCacheInfo(String keyName, String cacheName) {
        Map<String, Object> cacheInfo = monitorCacheService.getCacheInfo(keyName, cacheName);

        return OutJson.ok().put("cacheInfo", cacheInfo);
    }

    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/deleteCacheInfo")
    public OutJson deleteCacheInfo(String keyName, String cacheName) {
        monitorCacheService.deleteCacheInfo(cacheName, keyName);

        return OutJson.ok();
    }

    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    @RequiresPermissions("monitorCache:info")
    public OutJson info(@PathVariable("id") Long id) {
        MonitorCache monitorCache = monitorCacheService.queryObject(id);

        return OutJson.ok().put("monitorCache", monitorCache);
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    @RequiresPermissions("monitorCache:save")
    public OutJson save(@RequestBody MonitorCache monitorCache) {
        monitorCacheService.save(monitorCache);

        return OutJson.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("monitorCache:update")
    public OutJson update(@RequestBody MonitorCache monitorCache) {
        monitorCacheService.update(monitorCache);

        return OutJson.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("monitorCache:delete")
    public OutJson delete(@RequestBody Long[] ids) {
        monitorCacheService.deleteBatch(ids);

        return OutJson.ok();
    }

}
